<?php
/*

 All rights Reserved, Designed By www.youyacao.com
 @Description:首页
 @author:成都市一颗优雅草科技有限公司
 @version 优雅草松鼠聚合直播系统
 需要商业用途或者定制开发等可访问songshu.youyacao.com  联系QQ:2853810243 422108995 23625059584
 正版系统查询系统 zhengbancha.youyacao.com

*/
namespace app\admin\controller;

use \think\Db;
use think\Exception;
use \think\Request;
use \think\Image;

class Index extends Base
{
    public function index()
    {
        $data = array();
        $data['todayRegNum'] = $this->user['user_type'] == 'admin' ?
                    Db::name("user")->where("user_type","member")->where("reg_time",">=",strtotime("00:00"))->count() :
                    Db::name("user")->where("user_type","member")->where("reg_time",">=",strtotime("00:00"))->where("parent_id",$this->user['id'])->count();
        $data['todayActNum'] = $this->user['user_type'] == 'admin' ?
                    Db::name("cards")->where("actived",1)->where("active_time",">=",strtotime("00:00"))->count() :
                    Db::name("cards")->where("actived",1)->where("active_time",">=",strtotime("00:00"))->where("user_id",$this->user['id'])->count();
        $data['allRegNum'] = $this->user['user_type'] == 'admin' ?
                    Db::name("user")->where("user_type","member")->count():
                    Db::name("user")->where("user_type","member")->where("parent_id",$this->user['id'])->count();
        $data['allActNum'] = $this->user['user_type'] == 'admin' ?
                    Db::name("cards")->where("actived",1)->count() :
                    Db::name("cards")->where("user_id",$this->user['id'])->where("actived",1)->count();
        if($this->user['user_type'] == 'admin'){
            $data['adimages'] = Db::name("adimages")->select();
            $settings = Db::name("settings")->select();
            $data['settings'] = $settings;
            $adText = Db::name("adimages")->where("name","text")->find();
            if($adText) $data['adText'] = array('text'=>$adText['filepath'],'link'=>$adText['link_url']);
            $systemText = Db::name("adimages")->where("name","system")->find();
            if($adText) $data['systemText'] = array('text'=>$systemText['filepath'],'link'=>$systemText['link_url']);
        }else{
            $data['dailinotice'] = Db::name("settings")->where('set_name','dailinotice')->value("set_value");
        }
        $this->success($data);
    }

    public function save_settings(){
        $this->onlyAdmin();

        if(!request()->isPost()) $this->error('请求类型有误');
        $data = array();
        if(input('?apiurl')){
            if(strlen(input('apiurl'))==0) $this->error("请填写采集地址");
            $setRs = Db::name('settings')->where('set_name','apiurl')->find();
            if($setRs){
                if(Db::name('settings')->where('set_name','apiurl')->setField('set_value',input('apiurl'))){
                    $this->success(NULL,'保存成功');
                }else{
                    $this->error('未能更新数据');
                }
            }else{
                $data['set_name'] = 'apiurl';
                $data['set_value'] = input('apiurl');
                if(Db::name('settings')->insert($data)){
                    $this->success(NULL,'保存成功');
                }else{
                    $this->error('未能更新数据');
                }
            }
        }
        if(input('?byapiurl')){
            if(strlen(input('byapiurl'))==0) $this->error("请填写采集地址");
            $setRs = Db::name('settings')->where('set_name','byapiurl')->find();
            if($setRs){
                if(Db::name('settings')->where('set_name','byapiurl')->setField('set_value',input('byapiurl'))){
                    $this->success(NULL,'保存成功');
                }else{
                    $this->error('未能更新数据');
                }
            }else{
                $data['set_name'] = 'byapiurl';
                $data['set_value'] = input('byapiurl');
                if(Db::name('settings')->insert($data)){
                    $this->success(NULL,'保存成功');
                }else{
                    $this->error('未能更新数据');
                }
            }
        }
        if(input('?freetimes')){
            if(intval(input('freetimes'))<=0) $this->error("免费次数为大于0的整数数字");
            $setRs = Db::name('settings')->where('set_name','freetimes')->find();
            if($setRs){
                if(Db::name('settings')->where('set_name','freetimes')->setField('set_value',intval(input('freetimes')))){
                    $this->success(NULL,'保存成功');
                }else{
                    return $this->error('未能更新数据');
                }
            }else{
                $data['set_name'] = 'freetimes';
                $data['set_value'] = intval(input('freetimes'));
                if(Db::name('settings')->insert($data)){
                    $this->success(NULL,'保存成功');
                }else{
                    $this->error('未能更新数据');
                }
            }
        }

        $tuijian = input('?tuijian_1') ?  'tuijian_1' : (input('?tuijian_5') ?  'tuijian_5' : (input('?tuijian_10') ? 'tuijian_10' : NULL));
        if($tuijian){
            if(intval(input($tuijian))<=0) $this->error("奖励天数为大于0的整数");
            $setRs = Db::name('settings')->where('set_name',$tuijian)->find();
            if($setRs){
                if(Db::name('settings')->where('set_name',$tuijian)->setField('set_value',intval(input($tuijian)))){
                    $this->success(NULL,'保存成功');
                }else{
                    return $this->error('未能更新数据');
                }
            }else{
                $data['set_name'] = $tuijian;
                $data['set_value'] = intval(input($tuijian));
                if(Db::name('settings')->insert($data)){
                    $this->success(NULL,'保存成功');
                }else{
                    $this->error('未能更新数据');
                }
            }
        }

        $request = Request::instance();
        $param = $request->param();
        if($param){
            $allCount = count($param);
            $sucessCount = 0;
            foreach ($param as $key=>$value){
                $setRs = Db::name('settings')->where('set_name',$key)->find();
                if($setRs){
                    if(Db::name('settings')->where('set_name',$key)->setField('set_value',$value)){
                        $sucessCount++;
                    }
                }else{
                    $data['set_name'] = $key;
                    $data['set_value'] = $value;
                    if(Db::name('settings')->insert($data)){
                        $sucessCount++;
                    }
                }
            }
            if($sucessCount == 0) $this->error('未能更新数据');
            if($sucessCount < $allCount) $this->error('未能完全更新数据');
            $this->success(NULL,'保存成功');
        }

        $this->error("设置项错误");
    }

    public function save_qq(){
        $qq = input("qq");
        if(Db::name("user")->where("id",$this->user['id'])->setField("qq",$qq)){
            $this->success(NULL,"保存成功");
        }else{
            $this->error("未知错误，保存失败");
        }
    }

    public function alter_password(){
        if(!request()->isPost()) $this->error('请求类型有误');
        $oldpassword = input("oldpassword");
        $newpassword = input("newpassword");
        $newpassword2 = input("newpassword2");
        if(strlen($oldpassword)<6 || strlen($newpassword)<6 || strlen($newpassword2)<6) $this->error("密码不能小于6位");
        if(!Db::name("user")->where("id",$this->user['id'])->where("password",md5($oldpassword))->find()) $this->error("旧密码错误");
        if($newpassword != $newpassword2) $this->error("两次新密码不一致");
        if(Db::name("user")->where("id",$this->user['id'])->setField("password",md5($newpassword))){
            $this->success(NULL,"密码修改成功");
        }else{
            $this->error("密码修改失败");
        }
    }

    private function get_yqm($len=10){
        $str = "asdfghjklzxcvbnmoiuytrewqMNBVCXZASDFGHJKLPOIUYTREWQ0987654123";
        $yqm = "";
        for($i=0;$i<$len;$i++){
            $offset = mt_rand(0,strlen($str));
            $yqm .= substr($str,$offset,1);
        }
        return $yqm;
    }

    public function update_yqm(){
        $this->onlyAdmin();

        $value = $this->get_yqm();
        while(!Db::name('user')->where('id',$this->user['id'])->setField('tuijianma',$value)){
            $value = $this->get_yqm();
        }
        $this->success($value,'保存成功');
    }

    public function upload_image(){
        $this->onlyAdmin();

        $file = request()->file('file');
        $name = input("name");
        $link_url = input("link_url");
        $img_path = input("image_url");
        if($file){
            //图片可以是20M以内
            $info = $file->validate(['size'=>20480000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public/uploads');
            if($info){
                $exif = array();
                try{
                    $exif = exif_read_data(ROOT_PATH . 'public/uploads/'.$info->getSaveName());
                }catch (Exception $e){

                }

                $image = Image::open(ROOT_PATH . 'public/uploads/'.$info->getSaveName());

                if(!empty($exif['Orientation'])) {
                    switch($exif['Orientation']) {
                        case 8:
                            $image = $image->rotate(-90);
                            break;
                        case 3:
                            $image = $image->rotate(180);
                            break;
                        case 6:
                            $image = $image->rotate(90);
                            break;
                    }
                }

                $width = $image->width();
                $height = $image->height();
                $_height = $width ? $height/$width * 750 : 0;
                $filepath = $info->getSaveName();
                $filepath = str_replace("\\","/",$filepath);
                $image->thumb(750, $_height)->save(ROOT_PATH . 'public/uploads/'.$filepath);

                $data = Db::name("adimages")->where("name",$name)->find();
                if(!$data){
                    $data = array();
                    $data['name'] = $name;
                    $data['link_url'] = $link_url;
                    $data['filename'] = $info->getFilename();
                    $data['filepath'] = 'uploads/'.$filepath;
                    $data['upload_time'] = time();
                    if(Db::name("adimages")->insert($data)){
                        $this->success('uploads/'.$filepath,"保存成功");
                    }else{
                        unlink(ROOT_PATH . 'public/uploads/'.$filepath);//删除图片
                        $this->error("图片保存失败");
                    }
                }else{
                    $data['link_url'] = $link_url;
                    $data['filepath'] = 'uploads/'.$filepath;
                    $data['upload_time'] = time();
                    if(Db::name("adimages")->where("id",$data['id'])->update($data)){
                        $this->success('uploads/'.$filepath,"保存成功");
                    }else{
                        unlink(ROOT_PATH . 'public/uploads/'.$filepath);//删除图片
                        $this->error("图片保存失败");
                    }
                }




            }else{
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }else{
            $data = Db::name("adimages")->where("name",$name)->find();
            if(!$data){
                $data = array();
                $data['name'] = $name;
                $data['link_url'] = $link_url;
                $data['filename'] = "";
                $data['filepath'] = $img_path;
                $data['upload_time'] = time();
                if(Db::name("adimages")->insert($data)){
                    $this->success($img_path,"保存成功");
                }else{
                    $this->error("图片保存失败");
                }
            }else{
                $data['link_url'] = $link_url;
                $data['filepath'] = $img_path;
                $data['upload_time'] = time();
                if(Db::name("adimages")->where("id",$data['id'])->update($data)){
                    $this->success($img_path,"保存成功");
                }else{
                    $this->error("图片保存失败");
                }
            }


        }
        //$this->error("未上传图片");
    }

    public function delete_image(){
        $this->onlyAdmin();
        $id = input('id');
        if(input("?filename")){
            $image = Db::name("adimages")->where('filename',strtolower(input("filename")))->find();
        }else{
            $image = Db::name("adimages")->where('id',$id)->find();
        }

        if(!$image) $this->error('图片不存在');
        if(Db::name("adimages")->where("id",$image['id'])->delete()){
            unlink(ROOT_PATH . 'public/'.$image['filepath']);
            $this->success(NULL,'删除成功');
        }else{
            $this->error('图片删除失败');
        }
    }

    public function savelink(){
        if(!request()->isPost()) $this->error('请求类型有误');

        $id  = intval(input("id"));
        $month_card = input("month_card");
        $ji_card = input("ji_card");
        $year_card = input("year_card");
        $forever_card = input("forever_card");
        $data = Db::name("links")->where("user_id",$this->user['id'])->where("id",$id)->find();
        if(!$data){
            $data = array();
            $data['month_card'] = $month_card;
            $data['ji_card'] = $ji_card;
            $data['year_card'] = $year_card;
            $data['forever_card'] = $forever_card;
            $data = array('user_id'=>$this->user['id'],'links'=>serialize($data),'update_time'=>time());
            if(Db::name("links")->insert($data)){
                $this->success(NULL,"链接保存成功");
            }else{
                $this->error("链接保存失败");
            }
        }else{
            $links = array();
            $links['month_card'] = $month_card;
            $links['ji_card'] = $ji_card;
            $links['year_card'] = $year_card;
            $links['forever_card'] = $forever_card;
            if(Db::name("links")->where("id",$data['id'])->setField("links",serialize($links))){
                $this->success(NULL,"链接保存成功");
            }else{
                $this->error("链接保存失败");
            }
        }
    }

    public function getlinks(){
        $data = Db::name("links")->where("user_id",$this->user['id'])->find();
        $link = $data ? unserialize($data['links']) : array();
        $link['id'] = $data ? $data['id'] : 0;
        $this->success($link);
    }

    public function save_adtext(){
        $this->onlyAdmin();
        if(!request()->isPost()) $this->error('请求类型有误');
        $data = array();
        $data['filepath'] = input("text");
        $data['link_url'] = input("link");
        $data['upload_time'] = time();
        if(Db::name("adimages")->where("name","text")->find()){
            if(Db::name("adimages")->where("name","text")->update($data)){
                $this->success(NULL,"广告保存成功");
            }else{
                $this->error("广告保存失败");
            }
        }else{
            $data['name'] = 'text';
            $data['filename'] = 'text';
            if(Db::name("adimages")->insert($data)){
                $this->success(NULL,"广告保存成功");
            }else{
                $this->error("广告保存失败");
            }
        }
    }

    public function save_systemtext(){
        $this->onlyAdmin();
        if(!request()->isPost()) $this->error('请求类型有误');
        $data = array();
        $data['filepath'] = input("text");
        $data['link_url'] = input("link");
        $data['upload_time'] = time();
        if(Db::name("adimages")->where("name","system")->find()){
            if(Db::name("adimages")->where("name","system")->update($data)){
                $this->success(NULL,"系统提示保存成功");
            }else{
                $this->error("系统提示保存失败");
            }
        }else{
            $data['name'] = 'system';
            $data['filename'] = 'system';
            if(Db::name("adimages")->insert($data)){
                $this->success(NULL,"系统提示保存成功");
            }else{
                $this->error("系统提示保存失败");
            }
        }
    }
}
